/*
 * Copyright (c) 2008-2012
 * LANIT
 * All rights reserved.
 *
 * This product and related documentation are protected by copyright and
 * distributed under licenses restricting its use, copying, distribution, and
 * decompilation. No part of this product or related documentation may be
 * reproduced in any form by any means without prior written authorization of
 * LANIT and its licensors, if any.
 *
 * $Id$
 */

import javax.sql.DataSource;
import java.io.*;
import java.sql.*;

/**
 * todo Document type StartCode
 */
public class StartCode {

    public void writerToClob(Writer clobWriter, Reader readerFile) throws IOException {

        while (readerFile.ready()) {
            clobWriter.write(readerFile.read());

        }
        clobWriter.flush();
    }

    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {

        //boolean isFileExist;
        String fileName = "system.log";
        String path = "/";
        String distFile = path + fileName;
        String inputStr;
        String writeString = "Шалом";
        Boolean appendMode = true;
        FileFactory fileFactory = new FileFactory();
        StartCode startCode = new StartCode();

        DataSource dataSource;
        Connection connection;
        Statement statement;
        PreparedStatement preparedStatement;
        ResultSet resultSet;
        String querySQL;
        Clob clob = null;

        dataSource = new DataSourceFactory().getDataSource();


        connection = dataSource.getConnection();
        statement = connection.createStatement();
//        clob = new CLOB((OracleConnection) connection);

        resultSet = statement.executeQuery("SELECT * FROM testtable");

        while (resultSet.next()) {
            Println.toOut(resultSet.getString(2));
        }
        resultSet.close();

        resultSet = statement.executeQuery("insert into testtable(id,clobtest,blobtest) values (1, EMPTY_CLOB (), EMPTY_BLOB ())");

        resultSet.close();
        statement.close();



        statement = connection.createStatement();
        resultSet = statement.executeQuery("select clobtest from testtable where id=1 FOR UPDATE");
        while (resultSet.next()) {
            clob = resultSet.getClob("clobtest");
        }

        /*startCode.writerToClob(clob.setCharacterStream(1), fileFactory.getReader(distFile));

        resultSet.close();
        statement.close();
        connection.close();*/

        OutputStream clobOutputStream = clob.setAsciiStream(0);
        InputStream inputStreamFile = fileFactory.getInputStream(distFile);
        while (inputStreamFile.available() != 0) {
            clobOutputStream.write(inputStreamFile.read());
            clobOutputStream.flush();
        }
        clobOutputStream.close();
        inputStreamFile.close();
        resultSet.close();
        statement.close();
        connection.close();
    }
}
